import type * as VideoType from '../type';

function getrandomnumber(){
  const list = ["1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f"];
  let result = "";
  for( let i = 0; i<32; i++ ){
    result+= list[Math.floor(Math.random()*list.length)];
  }
  return result;
}

export function modeltoctfreehandrenderdata( result:  any ){
  const { vertices, uuid  } = result;
   const data = {
    active:false,
    canComplete: false,
    color: undefined,
    handles:{
      points: vertices,
      invalidHandlePlacement: false,
      textBox:{
        active: false,
        allowedOutsideImage: true,
        drawnIndependently: true,
        hasBoundingBox: true,
        hasMoved: false,
        movesIndependently: false,
      }
    },
    highlight: false,
    polyBoundingBox: {},
    invalidated: true,
    unit: "",
    uuid: uuid || getrandomnumber(),
    visible: true,
  }
  return data
}

export function modeltoctrectenagrenderdata( result:  VideoType.RectangModel){
  const { point1, point2, uuid } = result;
   const data = {
    active:false,
    color: undefined,
    // cachedStats,
    handles:{
      end: {
        active: false,
        highlight: true,
        moving: false,
        ...point2,
      },
      initialRotation: 0,
      start: {
        active: false,
        highlight: true,
        ...point1
      },
      // textBox: textBox,
    },
    invalidated: true,
    unit: "",
    uuid: uuid || getrandomnumber(),
    visible: true,
  }
  return data;
}



// 获取标注工具信息
export function getToolInfo(type:string){
  const rectangle = {
    name: "矩形",
    toolName: "RectangleRoi",
    iconName: "juxing1",
    ctRenderDataToModel: ctRenderDataToRectangModel,  //把cornerstone生成的数据转换成api接口参数的数据格式
    modeltoctrenderdata: modeltoctrectenagrenderdata,
    getLabelIndex: (result: VideoType.RectangModel | any )=>{
      return result?.z || result.point1?.z;
    }
    
  }
  const freehand = {
    name: "闭合画笔",
    toolName: "FreehandRoi",
    iconName: "huabi1",
    ctRenderDataToModel: ctRenderDataToFreehandModel,
    modeltoctrenderdata: modeltoctfreehandrenderdata,
    getLabelIndex: (result: VideoType.FreehandModel )=>{
      return result.z;
    }
  }

  const config:any = {
    RECTANGLE: rectangle,
    RectangleRoi: rectangle,
    FREEHANDLINECLOSED: freehand,
    FREEHANDLINE: freehand,
    FreehandRoi: freehand,
  }
  return config[type] || [null, null];
}

//cornerstonetool绘制的矩形数据 转成 api接口数据
export function ctRenderDataToRectangModel(data:any, z:number): any {
  const { cachedStats } = data;
  const { start, end, textBox } = data.handles || {};
  const point1: VideoType.RectangModelPoint = {
    x: start.x,
    y: start.y,
    // active: false,
    // highlight: start.highlight,
    z,            //当前帧
  };
  const point2: VideoType.RectangModelPoint = {
    x: end.x,
    y: end.y,
    // active: false,
    // highlight: end.highlight,
    // moving: end.moving,
    z,            //当前帧
  };
  const model: VideoType.RectangModel = { 
    point1,
    point2,
    // textBox,
    // uuid: data.uuid,
    // cachedStats
  }
  return { data: model, version: 1.0 };
}

export function ctRenderDataToFreehandModel(data:any, z:number): any {
  const { points } = data.handles || {};
  const model: VideoType.FreehandModel = { 
    vertices: points,
    version: 1,
    z,
    uuid: data.uuid,
  }
  return { data: model, version: 1.0};
}




